<?php

namespace Api\Model;

use Think\Model;

class PostModel extends Model {

    protected $tablename = 'classmember';

    function addPost($class_id, $user_id, $post_content, $media, $is_vote, $is_comment = 1) {
        $data = array(
            'class_id' => $class_id,
            'user_id' => $user_id,
            'post_content' => $post_content,
            'media' => $media,
            'is_vote' => $is_vote,
            'is_comment' => $is_comment,
            'post_content' => $post_content,
            'createtime' => date('Y-m-d H:i;s'),
        );
        $this->add($data);
    }

    function getlist($user_id, $class_id = 0, $start = 0, $limit = 20) {
        $where = 'jl_classmember.user_id=' . $user_id . ' AND (jl_post.is_hide=0 OR (jl_post.is_hide=1 AND jl_classmember.power >=jl_post.showid))';
        if ($class_id) {
            $where = 'jl_classmember.user_id=' . $user_id . 'AND jl_classmember.class_id =' . $class_id . ' AND (jl_post.is_hide=0 OR (jl_post.is_hide=1 AND jl_classmember.power >=jl_post.showid))';
        }
        $field = 'jl_post.id as post_id';
        $join1 = '__CLASSMEMBER__ on __POST__.class_id=__CLASSMEMBER__.class_id AND ';
        $join2 = '__ICONS__ on __POST__.id=__ICONS__.post_id';
        $join3 = '__CLASSMEMBER__ on __POST__.class_id=__CLASSMEMBER__.class_id';
        return $this->where($where)->join($join1)->join($join2)->field($field)->limit($limit)->select();
    }

    function getPostbypid($post_id) {
        $where = array('id' => $post_id);
        return $this->where($where)->find();
    }

    function getPostlistbycids($cids, $start = 0, $limit = 20) {
        $map['class_id'] = array('in', $cids);
        $limit = $start . ',' . $limit;
        return $this->where($map)->order('id desc')->limit($limit)->select();
    }

    function getPostlistbycid($cid, $start = 0, $limit = 20) {
        $where = array('class_id' => $cid);
        $limit = $start . ',' . $limit;
        return $this->where($where)->order('id desc')->limit($limit)->select();
    }

}
